<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use app\admin\model\Granary;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{
    
    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;
        $this->view->assign("payStatusList", $this->model->getPayStatusList());
        $this->view->assign("payTypeList", $this->model->getPayTypeList());
        $this->view->assign("specTypeList", $this->model->getSpecTypeList());
    }

    public function granary(){
        $user_id = input('ids');
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
               
               $update = [];
               foreach ($params['id'] as $key => $value) {
                   $update[$key]['id'] = $value;
                   $update[$key]['num'] = $params['num'][$key]; 
               }
               $Granary = new Granary();

               $res = $Granary->saveAll($update);
               if($res)$this->success('修改成功');                 
            }else{
                $this->error(__('Parameter %s can not be empty', ''));
            }
            
        }
        $list = Db::name('granary')->where('user_id',$user_id)->select();
        foreach ($list as $key => $value) {
            $list[$key]['fruit_name'] = Db::name('plant')->where('id',$value['plant_id'])->value('fruit_name');
        }        
      
        $this->view->assign("user_id", $user_id);
        $this->view->assign("data", $list);
        return $this->view->fetch();
    }

    public function fahuo(){
        $order_id = input('ids');
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
               $params['delivery_time'] = time();
               $params['pay_status'] = 30;

               $res = Db::name('order')->update($params);
               if($res)$this->success('发货成功');                 
            }else{
                $this->error(__('Parameter %s can not be empty', ''));
            }
            
        }
        $list = $this->model
            ->with('user')
            ->find($order_id);

        $list = $list->toArray();
      
        // halt($list);
        $this->view->assign("order_id", $order_id);
        $this->view->assign("data", $list);
        return $this->view->fetch();
    }
    
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with('user')
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with('user')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            // halt($list);
            $list = collection($list)->toArray();
            
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    

}
